package com.busi.wms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.busi.wms.model.WmsOutstorage;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 出库单主表
 */
public class WmsOutstorageController extends BaseController {
	
	public void listPage() {
		setAttr("dictDataout_type", WmsOutstorage.me.getDictDataout_type());
		setAttr("dictDatastorage_no", WmsOutstorage.me.getDictDatastorage_no());
		setAttr("dictDataoperate_type", WmsOutstorage.me.getDictDataoperate_type());
		setAttr("dictDataout_status", WmsOutstorage.me.getDictDataout_status());
		render("list.html");
	}
	
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_outstorage", properties, symbols, values, orderBy, getPager());
		Map<String, Object> dictDataout_type = WmsOutstorage.me.getDictDataout_type();
		for(Record record : list) {
			String fieldName = "out_type";
			if(dictDataout_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataout_type.get(record.get(fieldName).toString()));
			}
		}
		
		Map<String, Object> dictDatastorage_no = WmsOutstorage.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDataoperate_type = WmsOutstorage.me.getDictDataoperate_type();
		for(Record record : list) {
			String fieldName = "operate_type";
			if(dictDataoperate_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataoperate_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDataout_status = WmsOutstorage.me.getDictDataout_status();
		for(Record record : list) {
			String fieldName = "out_status";
			if(dictDataout_status.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataout_status.get(record.get(fieldName).toString()));
			}
		}
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_outstorage", properties, symbols, values)
		);
	}
	
	//增加页面
	public void addPage() {
		setAttr("dictDataout_type", WmsOutstorage.me.getDictDataout_type());
		setAttr("dictDatastorage_no", WmsOutstorage.me.getDictDatastorage_no());
		setAttr("dictDataoperate_type", WmsOutstorage.me.getDictDataoperate_type());
		setAttr("out_name",PropKit.get("comp_name")+"出库单");
		render("add.html");
	}
	
	//增加
	public void add() {
		getModel(WmsOutstorage.class, "model").save();
		
		addOpLog("[出库单主表] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {
		setAttr("dictDataout_type", WmsOutstorage.me.getDictDataout_type());
		setAttr("dictDatastorage_no", WmsOutstorage.me.getDictDatastorage_no());
		setAttr("dictDataoperate_type", WmsOutstorage.me.getDictDataoperate_type());
		setAttr("dictDataout_status", WmsOutstorage.me.getDictDataout_status());
		setAttr("model", WmsOutstorage.me.findById(getPara("id")));
		render("update.html");
	}
	//修改页面
	public void pubishPage() {
		WmsOutstorage model = WmsOutstorage.me.findById(getParaToInt("id"));
		Map<String, Object> dictDataout_type = WmsOutstorage.me.getDictDataout_type();
		if(dictDataout_type.get(model.get("out_type").toString()) != null) {
			model.set("out_type", dictDataout_type.get(model.get("out_type").toString()));
		}
		
		Map<String, Object> dictDatastorage_no = WmsOutstorage.me.getDictDatastorage_no();
		if(dictDatastorage_no.get(model.get("storage_no").toString()) != null) {
			model.set("storage_no", dictDatastorage_no.get(model.get("storage_no").toString()));
		}
		Map<String, Object> dictDataoperate_type = WmsOutstorage.me.getDictDataoperate_type();
		if(dictDataoperate_type.get(model.get("operate_type").toString()) != null) {
			model.set("operate_type", dictDataoperate_type.get(model.get("operate_type").toString()));
		}
		setAttr("dictDataout_status", WmsOutstorage.me.getDictDataout_status());
			setAttr("model",model);
			render("publish.html");
	}
	//修改
	public void update() {
		WmsOutstorage model = WmsOutstorage.me.findById(getPara("id"));
		model.set("out_type", getPara("model.out_type"));
		model.set("storage_no", getPara("model.storage_no"));
		model.set("out_time", getPara("model.out_time"));
		model.set("out_user", getPara("model.out_user"));
		model.set("audit_user", getPara("model.audit_user"));
		model.set("audit_time", getPara("model.audit_time"));
		model.set("print_user", getPara("model.print_user"));
		model.set("print_time", getPara("model.print_time"));
		model.set("operate_type", getPara("model.operate_type"));
		model.set("out_name", getPara("model.out_name"));
		model.set("out_no", getPara("model.out_no"));
		model.set("out_barcode", getPara("model.out_barcode"));
		model.set("out_target", getPara("model.out_target"));
		model.set("out_batch", getPara("model.out_batch"));
		model.update();
		addOpLog("[出库单主表] 修改");
		renderSuccess();
	}
	//修改
	public void publish() {
			WmsOutstorage model = WmsOutstorage.me.findById(getPara("id"));
			
			model.set("audit_user", getPara("model.audit_user"));
			model.set("audit_time", getPara("model.audit_time"));			
			model.set("out_status", getPara("model.out_status"));
			model.update();
			addOpLog("[出库单主表] 发布");
			renderSuccess();
		}
	//删除
	public void delete() {		
		WmsOutstorage.me.findById(getPara("id")).delete();		
		
		addOpLog("[出库单主表] 删除");
		renderSuccess();
	}
	//执行出库操作逻辑
	public void exeuOutStorage(){
		
		renderSuccess("未连接PLC");
	}
	//详情页面
	public void detailPage() {
		WmsOutstorage model = WmsOutstorage.me.findById(getParaToInt("id"));
		Map<String, Object> dictDataout_type = WmsOutstorage.me.getDictDataout_type();
		if(dictDataout_type.get(model.get("out_type").toString()) != null) {
			model.set("out_type", dictDataout_type.get(model.get("out_type").toString()));
		}
		
		Map<String, Object> dictDatastorage_no = WmsOutstorage.me.getDictDatastorage_no();
		if(dictDatastorage_no.get(model.get("storage_no").toString()) != null) {
			model.set("storage_no", dictDatastorage_no.get(model.get("storage_no").toString()));
		}
		Map<String, Object> dictDataoperate_type = WmsOutstorage.me.getDictDataoperate_type();
		if(dictDataoperate_type.get(model.get("operate_type").toString()) != null) {
			model.set("operate_type", dictDataoperate_type.get(model.get("operate_type").toString()));
		}
		Map<String, Object> dictDataout_status = WmsOutstorage.me.getDictDataout_status();
		if(dictDataout_status.get(model.get("out_status").toString()) != null) {
			model.set("out_status", dictDataout_status.get(model.get("out_status").toString()));
		}
		setAttr("model", model);
		render("detail.html");
	}
	
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_outstorage", properties, symbols, values);
		Map<String, Object> dictDataout_type = WmsOutstorage.me.getDictDataout_type();
		for(Record record : list) {
			String fieldName = "out_type";
			if(dictDataout_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataout_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatastorage_no = WmsOutstorage.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDataoperate_type = WmsOutstorage.me.getDictDataoperate_type();
		for(Record record : list) {
			String fieldName = "operate_type";
			if(dictDataoperate_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDataoperate_type.get(record.get(fieldName).toString()));
			}
		}
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("出库类型");
		clomuns.add("out_type");
		headers.add("仓库编号");
		clomuns.add("storage_no");
		headers.add("出库时间");
		clomuns.add("out_time");
		headers.add("出库人员");
		clomuns.add("out_user");
		headers.add("审核人员");
		clomuns.add("audit_user");
		headers.add("审核时间");
		clomuns.add("audit_time");
		headers.add("打印人员");
		clomuns.add("print_user");
		headers.add("打印时间");
		clomuns.add("print_time");
		headers.add("操作类型");
		clomuns.add("operate_type");
		headers.add("出库单名称");
		clomuns.add("out_name");
		headers.add("出库单编号");
		clomuns.add("out_no");
		headers.add("出库单条码");
		clomuns.add("out_barcode");
		headers.add("出库目标");
		clomuns.add("out_target");
		headers.add("出货批次");
		clomuns.add("out_batch");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("出库单主表");
		
		addOpLog("[出库单主表] 导出cvs");
		render(csvRender);
	}
}
